package com.lsongh.springboot2.service;

import com.lsongh.springboot2.entity.News;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

public interface NewsService extends JpaRepository<News,Integer> {
    //获取所有新闻（没有假删除的数据）
    @Query("select a from News a  where a.newsDelete = ?1 and a.newsName  like concat('%' ,?2 , '%')  order by a.newsId desc ")
    List<News> findAllNewsByName(Integer isDelete,String name);
    //获取所有新闻（没有假删除的数据）
    @Query("select a from News a  where a.newsDelete = ?1 order by a.newsId desc ")
    List<News> findAllNews(Integer isDelete);

    @Modifying(clearAutomatically = true)
    @Transactional
    @Query("update News as a set a.newsDelete = 1 where a.newsId = ?1")
    void deleteNewById(Integer id);

    @Modifying(clearAutomatically = true)
    @Transactional
    @Query("update News as a set a.newsLikes = a.newsLikes + 1 where a.newsId = ?1")
    void addNewLikesById(Integer id);

}
